<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <!-- <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="商品种类">
              <j-dict-select-tag v-model="queryParam.category" placeholder="请选择商品种类" />
            </a-form-item>
          </a-col>




          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form> -->


    <a-form layout="inline" @keyup.enter.native="searchQuery">
      
      <a-row :gutter="24">
        <a-col :md="6" :sm="8">
          <a-form-item label="商品种类">
          <a-select style="width: 120px" @change="handleChangeCategoris" placeholder="请选择商品类别">
            <a-select-option v-for="categories in categoriesList" :value="categories.id" :key="categories.classificationName" >
              {{categories.classificationName}}
            </a-select-option>
          </a-select>
          
          <a-form-item :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">
            <j-input placeholder v-model="queryParam.category" hidden></j-input>
          </a-form-item>
          
          
          </a-form-item>
        </a-col>

        <a-col :md="6" :sm="8">
          <!-- <a-button type="primary" @click="searchGoods" icon="search">查询</a-button> -->
          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
          </span>
        </a-col>
      </a-row>
    </a-form>

    </div>

    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('商品管理表')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>



      <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns" :dataSource="dataSource"
        :pagination="ipagination" :loading="loading" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap" @change="handleTableChange">

        <!-- <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template> -->
        <!-- <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template> -->
        <!-- <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template> -->

        <template slot="piclot" slot-scope="text, record">
          <viewer>
            <img :src="record.commodityImg" width="200" height="140" />
          </viewer>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="getInfo(record)">详情</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a v-if="record.status == 1" @click="offTheShelf(record)">下架</a>
          <a v-else @click="shelves(record)">上架</a>
        </span>

      </a-table>
    </div>

    <commodity-management-modal ref="modalForm" @ok="modalFormOk"></commodity-management-modal>
    
    <a-modal v-model="visible" title="详情" @ok="handleOk">
         <p>商品名称: &nbsp; {{info.commodityName}}</p>
         <p>所属分类: &nbsp; {{info.classificationName}}</p>
         <p>商品图片: &nbsp;            
           <viewer >
               <img :src="info.commodityImg" width="100%" height="100%" />
           </viewer>
         </p>
         <p>需要积分: &nbsp; {{info.integral}}</p>
         <p>需要金额: &nbsp; {{info.amount}}</p>
         <p>上架/下架: &nbsp; {{info.status_dictText}}</p>

    </a-modal>
  </a-card>
</template>

<script>
  import '@/assets/less/TableExpand.less'
  import {
    mixinDevice
  } from '@/utils/mixin'
  import {
    JeecgListMixin
  } from '@/mixins/JeecgListMixin'
  import CommodityManagementModal from './modules/CommodityManagementModal'
  import JInput from '@comp/jeecg/JInput'
    import JImageUpload from '../../components/jeecg/JImageUpload'


  export default {
    name: "CommodityManagementList",
    mixins: [JeecgListMixin, mixinDevice],
    components: {
      CommodityManagementModal,
      JInput,
      JImageUpload
    },
    data() {
      return {
        info: {},
        visible: false,
        //商品分类列表
        categoriesSelect: '',
        categoriesList: [],
        description: '商品管理表管理页面',
        // 表头
        columns: [{
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: "center",
            customRender: function(t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '商品名称',
            align: "center",
            dataIndex: 'commodityName'
          },
          {
            title: '所属分类',
            align: "center",
            dataIndex: 'classificationName'
          },
          {
            title: '商品图片',
            align: "center",
            dataIndex: 'commodityImg',
            scopedSlots: {
              customRender: 'piclot'
            }
          },
          {
            title: '需要的积分',
            align: "center",
            dataIndex: 'integral'
          },
          {
            title: '需要的金额',
            align: "center",
            dataIndex: 'amount'
          },
          {
            title: '上/下架状态',
            align: "center",
            dataIndex: 'status_dictText'
          },
          {
            title: '删除状态',
            align: "center",
            dataIndex: 'delFlag_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: "center",
            fixed: "right",
            width: 147,
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],
        url: {
          list: "/commodityManagement/commodityManagement/findAll",
          delete: "/commodityManagement/commodityManagement/delete",
          deleteBatch: "/commodityManagement/commodityManagement/deleteBatch",
          exportXlsUrl: "/commodityManagement/commodityManagement/exportXls",
          importExcelUrl: "commodityManagement/commodityManagement/importExcel",
        },
        dictOptions: {},
        categoriesList: []
      }
    },
    created() {
      this.initCategories();
    },
    computed: {
      importExcelUrl: function() {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      handleChangeCategoris(value) {
        this.categoriesSelect = value;
        this.queryParam.category = value;
        console.log("分類ID:" + this.categoriesSelect);
      },
      initDictConfig() {},
      initCategories() {
        console.log("1111");
        this.axios.get('/categories/categories/findAll').then((res) => {
          this.categoriesList = res.result;
          console.log(this.categoriesList);
        })
      },
      offTheShelf(record) {
        this.axios.post('/commodityManagement/commodityManagement/offTheShelf', record).then((res) => {
          this.loadData();
        })
      },
      shelves(record) {
        console.log(record);
        this.axios.post('/commodityManagement/commodityManagement/shelves', record).then((res) => {
          this.loadData();
          // this.initIntegralData();
        })
      },
      searchGoods() {
        this.axios.get("/commodityManagement/commodityManagement/findAll",{params:{category:this.categoriesSelect}}).then((res) => {
          this.loadData();
          console.log(res);
        })
      },
      getInfo(record){
        this.info = record;
        this.visible = true;
      },
      handleOk(e) {
        console.log(e);
        this.info = {};
        this.visible = false;
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
